﻿
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>


@Html.DropDownList("ListCate", null, "chọn category", new { @class = "form-control", @onchange = "ListCateChange(this.value)" })
<br/>
@Html.DropDownList("ddlProduct", Enumerable.Empty<SelectListItem>(), "----chon product", new { @class = "form-control" })

@section scripts{
    <script type="text/javascript">
        function ListCateChange(val) {
            if (val != "" && val != undefined) {
                getproduct(val);
            } else {
                $("#ddlProduct")
                    .html($("<option>---- chon product</option>")
                        .attr("value", "")
                        .text(this.Name));
            }
        }

        function getproduct(idproduct) {
            $.ajax({
                type: "GET",
                url: '/Drowdowlist/GetByproduct',
                data: { idproduct: idproduct },
                dataType: "json",
                success: function (data) {
                    if (data.foo != "") {
                        var appenddata;
                        $.each(data.foo, function (key, value) {
                            appenddata += "<option value = '" + value.Id + " '>" + value.Name + " </option>";
                        });
                        $("#ddlProduct").html(appenddata);
                    } else {
                        $("#ddlProduct")
                            .html($("<option>---- chon product</option>")
                                .attr("value", "")
                                .text(this.Name));
                    }
                },
                error: function () {
                    alert("Da co loi xay ra");
                }
            });
        }
    </script>
}